<template>
  <div class="app-container">
    <span class="title">项目组装</span>
    <el-divider></el-divider>
    <div class="filter-container">
      <el-form :inline="true" label-position="right" label-width="90px">
        <el-form-item label="项目名称">
          <el-input v-model="listQuery.projectName" placeholder="项目名称" />
        </el-form-item>
        <el-form-item label="权利人">
          <el-input v-model="listQuery.realName" placeholder="权利人" />
        </el-form-item>
        <el-form-item label="身份证号码">
          <el-input v-model="listQuery.applicantIdCardNum" placeholder="身份证号码" />
        </el-form-item>
        <el-form-item label="项目级别">
          <el-select placeholder="项目级别" v-model="listQuery.level" clearable>
            <el-option label="区县级" value="DISTRICT" />
            <el-option label="市级" value="CITY" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="changePage(1)" type="primary">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="filter-container">
      <router-link :to="{ name: '土地储备项目组装添加' }">
        <el-button class="filter-item" type="primary" icon="el-icon-plus">添加</el-button>
      </router-link>
      <el-button @click="handleDelBatch" class="filter-item" style="margin-left: 10px;" type="danger">删除所选</el-button>
    </div>

    <el-table @selection-change="handleSelectionChange" v-loading="listLoading" :data="list" element-loading-text="加载中" highlight-current-row>
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="number" label="项目编号"></el-table-column>
      <el-table-column prop="name" label="项目名称"></el-table-column>
      <el-table-column label="项目级别">
        <template slot-scope="scope">{{ scope.row.levelDto.name }}</template>
      </el-table-column>
      <el-table-column label="指标使用方向">
        <template slot-scope="scope">{{ scope.row.projectIndex.name }}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <router-link :to="{ path: '/tdcb/xmprezz/detail?projectId=' + scope.row.id }">
            <el-button size="mini" type="primary">查看</el-button>
          </router-link>
          <!-- <router-link :to="{ path: '/xmkgh/xmprezz/edit' }">
            <el-button style="margin-left: 10px;" type="text">编辑</el-button>
          </router-link> -->
          <el-button size="mini" style="margin-left: 10px;" type="danger" @click="handleDelSingle(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination @current-change="changePage" :page-size="listQuery.size" layout="prev, pager, next, jumper" :total="total"></el-pagination>
    </div>
  </div>
</template>

<script>
import { getList, del } from "@/api/project/xmkgh/xmprezz";
import axios from "axios";

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger"
      };
      return statusMap[status];
    }
  },
  data() {
    return {
      list: null,
      total: null,
      listLoading: false,
      listQuery: {
        page: 1,
        size: 10,
        projectName: null,
        realName: null,
        level: null,
        applicantIdCardNum: null
      },
      landId: ""
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      getList(this.listQuery)
        .then(res => {
          this.listLoading = false;
          this.list = res.data.rows;
          this.total = res.data.total;
        })
        .catch(err => {
          this.listLoading = false;
        });
    },
    changePage(page) {
      this.listQuery.page = page;
      this.fetchData();
    },
    handleSelectionChange(arr) {
      let _arr = [];
      arr.forEach(item => {
        _arr.push(item.id);
      });
      this.landId = _arr.join(",");
    },
    handleDelSingle(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.listLoading = true;
        del({
          projectIds: row.id
        })
          .then(res => {
            this.listLoading = false;
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.fetchData();
          })
          .catch(err => {
            this.listLoading = false;
          });
      });
    },
    handleDelBatch() {
      if (this.landId == "") {
        this.$alert("请至少选择一条数据", "提示");
        return;
      }
      this.$confirm("此操作将永久删除所选数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.listLoading = true;
        del({
          projectIds: this.landId
        })
          .then(res => {
            this.listLoading = false;
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.fetchData();
          })
          .catch(err => {
            this.listLoading = false;
          });
      });
    }
  }
};
</script>
